package com.zcf.controller;

import com.zcf.dto.PasswordDto;
import com.zcf.pojo.Result;
import com.zcf.pojo.User;
import com.zcf.service.UserService;
import com.zcf.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.URL;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author zcf
 */
@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    @Resource
    private UserService userService;
    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result register(@Pattern(regexp = "^\\S{5,16}$") String username,@Pattern(regexp = "^\\S{5,16}$") String password) {
     return userService.register(username,password);
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result login(@Pattern(regexp = "^\\S{5,16}$") String username,@Pattern(regexp = "^\\S{5,16}$")String password){
     return userService.login(username,password);
    }


    @GetMapping("/userInfo")
    public Result userInfo(){
      Map<String,Object> map= ThreadLocalUtil.get();
     String username=(String) map.get("username");
      return userService.userInfo(username);
    }


    @PutMapping("/update")
    public Result update(@RequestBody User user){
        return userService.update(user);
    }


    @PutMapping("/updatePwd")
    public Result updatePassword(@RequestBody @Validated PasswordDto passwordDto,@RequestHeader("Authorization") String token){
      return userService.updatePassword(passwordDto,token);
    }


    @PatchMapping("/updateAvatar")
    public Result updateAvatar(@URL String avatarUrl){
          userService.updateUrl(avatarUrl);
          return Result.success();
    }
}
